package lee.vioson.ebook.ui.components

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.KeyboardArrowLeft
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.intl.Locale
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import androidx.navigation.NavHostController
import kotlinx.coroutines.flow.launchIn
import lee.vioson.composecommon.isNotEmpty
import lee.vioson.ebook.R
import lee.vioson.ebook.data.AppContainer
import lee.vioson.ebook.ui.AppTheme
import lee.vioson.ebook.viewmodels.OtpVeriViewModel

/**
 *Author:viosonlee
 *Date:2023/6/8
 *DESCRIPTION:
 */
@Composable
fun OtpVerification(
    navHostController: NavHostController,
    appContainer: AppContainer,
    email: String,
    password: String,
    type: Int,
    viewModel: OtpVeriViewModel = viewModel(factory = viewModelFactory {
        initializer { OtpVeriViewModel(appContainer, type) }
    }),
) {
    val state by viewModel.state.collectAsState()
    LaunchedEffect(key1 = Unit, block = {
        viewModel.sendEmailCode(email)
    })

    MviScreen(viewModel = viewModel) {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(horizontal = 16.dp),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Row(
                Modifier
                    .fillMaxWidth()
                    .statusBarsPadding()
            ) {
                Box(
                    modifier = Modifier
                        .size(48.dp)
                        .clip(CircleShape)
                        .background(AppTheme.colors.onBackground)
                        .clickable {
                            navHostController.popBackStack()
                        },
                    contentAlignment = Alignment.Center
                ) {
                    Icon(
                        imageVector = Icons.Rounded.KeyboardArrowLeft,
                        contentDescription = "back",
                        modifier = Modifier.size(36.dp)
                    )
                }
            }
            Spacer(modifier = Modifier.height(50.dp))
            Text(
                text = stringResource(id = R.string.Otp_Verification),
                style = TextStyle(fontSize = 28.sp, color = AppTheme.colors.onBackground)
            )
            Spacer(modifier = Modifier.height(24.dp))
            Text(
                text = stringResource(id = R.string.ov_tips),
                fontSize = 18.sp,
                color = AppTheme.appColors.loginTipsTextColor
            )
            Spacer(modifier = Modifier.height(50.dp))
            OtpTextField(otpText = state.otpText,
                otpCount = 4,
                onOtpTextChange = { str, complete ->
                    viewModel.sendEvent(OtpVeriViewModel.Event.OnOtpTxtChange(str, complete))
                })
            if (state.error.isNotEmpty) {
                Spacer(modifier = Modifier.height(16.dp))
                Text(
                    text = state.error!!,
                    style = TextStyle(fontSize = 16.sp, color = AppTheme.colors.error)
                )
            }
            Spacer(modifier = Modifier.height(48.dp))
            val language = Locale.current.language
            val areaCode = Locale.current.region
            Button(
                modifier = Modifier.height(54.dp),
                shape = RoundedCornerShape(size = 16.dp),
                colors = AppTheme.loginButtonColors(),
                enabled = state.completed,
                onClick = {
                    viewModel.commit(
                        email,
                        password,
                        state.otpText,
                        areaCode,
                        language,
                        navHostController
                    )
                }) {
                Row(verticalAlignment = Alignment.CenterVertically) {
                    Text(text = stringResource(id = R.string.Confirm), fontSize = 18.sp)
                }
            }
            Spacer(modifier = Modifier.height(36.dp))
            Text(
                modifier = Modifier.clickable { viewModel.resend(email) },
                text = if (state.lastSecond >= 0) stringResource(
                    id = R.string.Didnt_receive_code,
                    state.lastSecond
                ) else stringResource(
                    id = R.string.Didnt_receive_code_default
                ),
                fontSize = 18.sp,
                color = AppTheme.appColors.loginTipsTextColor
            )
        }
    }
}
